package com.jintian.smart.kernel.switching.config;

import com.jintian.smart.kernel.switching.fn.Contains;
import com.jintian.smart.kernel.switching.fn.DbRow2Json;
import com.jintian.smart.kernel.switching.fn.Split;
import com.jintian.smart.kernel.switching.runtime.db.DBPacketExtractTask;
import com.jintian.smart.kernel.switching.runtime.db.DBPacketLoadTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@ConditionalOnClass({DBPacketExtractTask.class, DBPacketLoadTask.class})
@ConditionalOnProperty(name = "mda.switching.enable", havingValue = "true", matchIfMissing = false)
@ComponentScan("com.jintian.smart.kernel.switching")
@EnableScheduling
public class SwitchingConfig {
    @Bean
    DBPacketExtractTask dbPacketExtractTask() {
        return new DBPacketExtractTask();
    }

    @Bean
    DBPacketLoadTask dbPacketLoadTask() {
        return new DBPacketLoadTask();
    }

    @Configuration
    static class ScheduledTask {
        @Autowired
        private DBPacketExtractTask extractTask;

        @Autowired
        private DBPacketLoadTask loadTask;

        @Scheduled(cron = "${smart.switching.extract.scheduler.db:-}")
        void dbPacketExtract() {
            extractTask.execute();
        }

        @Scheduled(cron = "${smart.switching.load.scheduler.db:-}")
        void dbPacketLoad() {
            loadTask.execute();
        }
    }

    @Bean
    Contains contains() {
        return new Contains();
    }

    @Bean
    Split split() {
        return new Split();
    }

    @Bean
    DbRow2Json dbRow2Json() {
        return new DbRow2Json();
    }
}